package com.atguigu03.threadsafe.runnablesafe;



class SaleTickets implements Runnable {

    int tickets = 100;
    Object obj = new Object();
    @Override
    public void run() {
        while (true) {

//            synchronized (obj) {
//            synchronized (this) {
            synchronized (SaleTickets.class) {
                if(tickets > 0) {
                    try {
                        Thread.sleep(10);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(Thread.currentThread().getName() + ":" + tickets);
                    tickets--;
                } else {
                    break;
                }
            }


        }
    }
}
public class TicketsTest {

    public static void main(String[] args) {

        SaleTickets saleTickets = new SaleTickets();
        Thread thread = new Thread(saleTickets);
        Thread thread1 = new Thread(saleTickets);
        Thread thread2 = new Thread(saleTickets);
        thread.start();
        thread1.start();
        thread2.start();

    }
}
